//
//  ModifyInfoController.m
//  healthApp
//
//  Created by william mu on 12-8-29.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "ModifyInfoController.h"

#define DEFAULT_STR @"选择或输入"

@interface ModifyInfoController ()

@end

@implementation ModifyInfoController
@synthesize picker;
@synthesize fieldName;
@synthesize fieldValue;
@synthesize param;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(CGPoint) getFieldRange:(NSString*) field
{
//    CGPoint range;
//    
//    if([field isEqual:@"height"])
//    {
//        range.x = 30;
//        range.y = 300;
//    }
//    else if([field isEqual:@"weight"])
//    {
//        range.x = 1;
//        range.y = 300;
//    }
//    else if([field isEqual:@"waistline"])
//    {
//        range.x = 50;
//        range.y = 100;
//    }
//    else if([field isEqual:@"bloodPressureHigh"])
//    {
//        range.x = 80;
//        range.y = 200;
//    }
//    else if([field isEqual:@"bloodPressureLow"])
//    {
//        range.x = 80;
//        range.y = 200;
//    }
//    else if([field isEqual:@"totalCholesterol"])
//    {
//        range.x = 1;
//        range.y = 10;
//    }
//    else if([field isEqual:@"highDensityLipoprotein"])
//    {
//        range.x = 1;
//        range.y = 5;
//    }
//    else if([field isEqual:@"lowDensityLipoprotein"])
//    {
//        range.x = 1;
//        range.y = 5;
//    }
//    else if([field isEqual:@"triglyceride"])
//    {
//        range.x = 1;
//        range.y = 3;
//    }
//    else if([field isEqual:@"fastingBloodSugar"])
//    {
//        range.x = 100;
//        range.y = 200;
//    }
//    else if([field isEqual:@"normalBloodSugar"])
//    {
//        range.x = 100;
//        range.y = 200;
//    }
//    else if([field isEqual:@"smoking"])
//    {
//        range.x = 0;
//        range.y = 1;
//    }
//    else if([field isEqual:@"drinkingWeekFreq"])
//    {
//        range.x = 0;
//        range.y = 20;
//    }
//    else if([field isEqual:@"doseOfDrink"])
//    {
//        range.x = 1;
//        range.y = 3;
//    }
//    else if([field isEqual:@"sportsWeekFreq"])
//    {
//        range.x = 0;
//        range.y = 20;
//    }
//    else if([field isEqual:@"sportsDuration"])
//    {
//        range.x = 1;
//        range.y = 3;
//    }
//    else if([field isEqual:@"pressure"])
//    {
//        range.x = 1;
//        range.y = 3;
//    }
//    else {
//        NSAssert(false, @"invalid field!");
//    }
    
//    return range;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"更新健康信息";
    
    m_fieldName = [self.param objectForKey:@"fieldName"];
    m_field = [self.param objectForKey:@"field"];
    m_fieldValue = [self.param objectForKey:@"fieldValue"];
    m_pickerData = nil;
    
    //self.fieldValue.inputView = self.picker;
    self.fieldName.text = m_fieldName;
    self.fieldValue.text = m_fieldValue;
    
    [self initPicker:m_field value:m_fieldValue];
}

-(void) viewWillDisappear:(BOOL)animated
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    GlobalState* state = [GlobalState sharedInstance];
    
    NSString* seq = [protocol updateHealthInfo:state.uid field:m_field value:self.fieldValue.text];
    [m_allRequest addObject:seq];
    
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:self.fieldValue.text forKey:@"value"];
    [dict setObject:m_field forKey:@"field"];
    [center postNotificationName:FIELD_UPDATE object:self userInfo:dict];
    
    [protocol shutdownAnyResponse:m_allRequest];
}

-(NSInteger) fieldMin:(NSString*) field
{
    if([field isEqual:@"smoking"])
        return 0;
    
    if([field isEqual:@"pressure"])
        return 1;
    
    if([field isEqual:@"sportsDuration"])
        return 1;
    
    if([field isEqual:@"doseOfDrink"])
        return 1;
    
    return 0;
}

-(NSInteger) fieldMax:(NSString*) field
{
    if([field isEqual:@"smoking"])
        return 1;
    
    if([field isEqual:@"pressure"])
        return 3;
    
    if([field isEqual:@"sportsDuration"])
        return 3;
    
    if([field isEqual:@"doseOfDrink"])
        return 3;
    
    
    return NSIntegerMax;
}

-(void) initPicker:(NSString*) field  value:(NSString*) value
{
    NSMutableArray* ary = [NSMutableArray array];
    [ary addObject:DEFAULT_STR];
    
    int val = [value intValue];
    NSInteger min = [self fieldMin:field];
    NSInteger max = [self fieldMax:field];
    for(int i = val - 5; i < val + 5; i++)
    {
        if(i < min)
            continue;
        
        if(i > max)
            continue;
        
        NSString* label = [NSString stringWithFormat:@"%d", i];
        [ary addObject:label];
    }
    
    
    m_pickerData = [ary retain];
    
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.picker = nil;
    self.fieldName = nil;
    self.fieldValue = nil;
    self.param = nil;
    [m_pickerData release], m_pickerData = nil;
}

-(void) dealloc
{
    
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return [m_pickerData count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return [m_pickerData objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    NSString* title = [m_pickerData objectAtIndex:row];
    
    if([title isEqual:DEFAULT_STR])
        return;
    
    self.fieldValue.text = title;
    
    //report data here!
}

@end
